package com.smartcruise.report.reportRecord;

import android.text.TextUtils;

import com.smartcruise.base.RefreshContract;
import com.smartcruise.data.PageHistory;
import com.smartcruise.data.ReportData;
import com.smartcruise.util.Key;
import com.smartcruise.util.TempleUser;
import com.smartcruise.util.network.callBack.SimpleCallBack;
import com.smartcruise.util.network.netUtils.NetCollection;
import com.smartcruise.util.network.netUtils.Result;

import io.realm.ObjectChangeSet;
import io.realm.Realm;
import io.realm.RealmModel;
import io.realm.RealmObjectChangeListener;
import io.realm.RealmResults;
import io.realm.Sort;

/**
 * Created by FengChaoQun
 * on 2018/1/20 0020
 */

public class ReportPresenterImp implements RefreshContract.Presenter {

    private RefreshContract.View mView;
    private ReportAdapter adapter;
    private String cruiseId;
    private PageHistory pageHistory;
    private RefreshContract.RefreshInterface refreshInterface;

    public ReportPresenterImp(RefreshContract.View mView, String cruiseId) {
        this.mView = mView;
        this.cruiseId = cruiseId;
        refreshInterface = mView.getRefresh();
    }

    @Override
    public void start() {
        pageHistory = mView.getRealm().where(PageHistory.class)
                .equalTo(Key.userId, TempleUser.getInstance().getAccount())
                .equalTo(Key.type, PageHistory.TYPE_REPORT)
                .findFirst();

        if (pageHistory == null) {
            mView.getRealm().executeTransaction(new Realm.Transaction() {
                @Override
                public void execute(Realm realm) {
                    PageHistory newPageHistory = new PageHistory();
                    newPageHistory.setType(PageHistory.TYPE_REPORT);
                    newPageHistory.setUserId(TempleUser.getInstance().getAccount());
                    pageHistory = realm.copyToRealmOrUpdate(newPageHistory);
                }
            });
        }

        pageHistory.addChangeListener(new RealmObjectChangeListener<RealmModel>() {
            @Override
            public void onChange(RealmModel realmModel, ObjectChangeSet changeSet) {
                refreshInterface.setNoMoreData(!pageHistory.hasNextData());
            }
        });

        if (TextUtils.isEmpty(cruiseId)) {
            RealmResults<ReportData> all = mView.getRealm().where(ReportData.class)
                    .equalTo(Key.inspectorId, TempleUser.getInstance().getAccount())
                    .findAllSorted(Key.createTime, Sort.DESCENDING);
            adapter = new ReportAdapter(all, mView.getActivity());
            mView.setData(adapter);
        } else {
            RealmResults<ReportData> all = mView.getRealm().where(ReportData.class)
                    .equalTo(Key.inspectorId, TempleUser.getInstance().getAccount())
                    .equalTo(Key.patrollingTaskId, cruiseId)
                    .findAllSorted(Key.createTime, Sort.DESCENDING);
            adapter = new ReportAdapter(all, mView.getActivity());
            mView.setData(adapter);
            refreshInterface.setEnableRefresh(false);
            refreshInterface.setNoMoreData(true);
        }

        if (pageHistory.needRefresh()) {
            refreshInterface.autoRefresh();
        }
    }

    /**
     * description:下拉刷新
     */
    @Override
    public void refresh() {
        NetCollection.getReportDataHistory(1, mView, new SimpleCallBack(mView) {
            @Override
            public void onSuccess(Result result) {
            }
        });
    }

    @Override
    public void loadMore() {
        NetCollection.getReportDataHistory(pageHistory.getCurrentPage() + 1, mView, new SimpleCallBack(mView) {
            @Override
            public void onSuccess(Result result) {
            }
        });
    }

    /**
     * description:界面销毁的时候调用
     */
    @Override
    public void destroy() {
        if (pageHistory != null) {
            pageHistory.removeAllChangeListeners();
        }
    }

}
